package cn.com.surker.workcon.service.zhcl;

import cn.com.surker.base.BaseEntity;
import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.common.Code;
import cn.com.surker.lab.entity.LabExpendUpload;
import cn.com.surker.production.entity.vo.ProductionIndentVo;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.RandomUtils;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.service.TaskService;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 珠海长路工控机的任务单 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2024-09-26
 */
public class TaskServiceImpl extends GKJDBCService implements TaskService {

	public int insert(Long lineId, ProductionIndentVo indent, Date datTim) {
		if (lineId == null || indent == null) {
			System.err.println("没有生产线(" + lineId + ")或者任务单信息(" + indent + ")");
			return Code.FAILED.getCode();
		}
		//	获取生产线
		SysLine line = getLine(lineId);
		if (!BaseEntity.validateExist(line)) {
			//	没有数据
			return Code.FAILED.getCode();
		}
		//	获取连接
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		//	任务单编号
		String indentCode = indent.getCode();
		//	根据任务单编号,查询是否存在上传数据
		String productTaskTable = ZhclUtil.getCtrlTableName(line, "ProductTask");
		String queryOldTask = "select id from " + productTaskTable + " where Code = '" + indentCode + "'";
		//	查询数据
		List<Map<String, Object>> oldTasks = lineJdbcTemp.queryForList(queryOldTask);
		if (oldTasks != null && oldTasks.size() > 0 && oldTasks.get(0) != null) {
			//	已经上传过
			return 1;
		}
		//	装配数据,需要先判断是否存在客户,如果没有存在客户,要先插入客户信息
		//	工程名称
		String conName = indent.getConName();
		//	工程编码
		String conCode = indent.getConCode();
		//	工程地址
		String address = indent.getAddress();
		//	客户名称
		String clientName = indent.getClientName();
		//	客户编码
		String clientCode = indent.getClientCode();
		//	工程id
		Long projectId = null;
		String cliProjectTable = ZhclUtil.getCtrlTableName(line, "Cli_Project");
		String queryConSql = "select id from " + cliProjectTable + " where Code = '" + conCode + "'";
		List<Map<String, Object>> conList = lineJdbcTemp.queryForList(queryConSql);
		if (conList == null || conList.isEmpty()) {
			//	没有合同需要插入,插入前需要查询客户
			String creator = indent.getCreator();
			if (creator == null) {
				creator = "erp用户";
			}
			//	客户id
			Long customerId = null;
			String clientTable = ZhclUtil.getCtrlTableName(line, "Client");
			String queryClientSql = "select id from " + clientTable + " where Code = '" + clientCode + "'";
			List<Map<String, Object>> cusList = lineJdbcTemp.queryForList(queryClientSql);
			if (cusList == null || cusList.isEmpty()) {
				//	查询最大的id值
				String maxClientIdSql = "select MAX(id) maxId from Client";
				List<Map<String, Object>> clDatas = lineJdbcTemp.queryForList(maxClientIdSql);
				if (clDatas == null || clDatas.isEmpty()) {
					customerId = 0l;
				} else {
					customerId = Long.valueOf(clDatas.get(0).get("maxId").toString());
				}
				customerId = customerId + 1;
				//	没有客户,需要插入客户数据
				String insertCusSql = "insert into " + clientTable + " (id,Code,Name,IfValid,Address,LinkMan,Phone,eMail,Remark) values (?,?,?,?,?,?,?,?,?)";
				Object[] cusParams = {customerId, clientCode, clientName, 1, null, null, null, null, creator};
				lineJdbcTemp.update(insertCusSql, cusParams);
			} else {
				//	取出客户id
				// customerId = Long.valueOf(String.valueOf(cusList.get(0).get("id")));
			}
			String maxProjectIdSql = "select MAX(id) maxId from " + cliProjectTable;
			List<Map<String, Object>> projDatas = lineJdbcTemp.queryForList(maxProjectIdSql);
			if (projDatas == null || projDatas.isEmpty()) {
				projectId = 0l;
			} else {
				projectId = Long.valueOf(projDatas.get(0).get("maxId").toString());
				;
			}
			//	最大id+1
			projectId = projectId + 1;
			//	拼接工程参数
			String insertConSql = "insert into " + cliProjectTable + " (ID,Client_Code,Code,Name,Address,Way,LinkMan,Phone,Remark) values (?,?,?,?,?,?,?,?,?)";
			Object[] proParams = {projectId, clientCode, conCode, conName, null, null, null, null, creator};
			//	插入工程数据
			lineJdbcTemp.update(insertConSql, proParams);
		} else {
			//	取出工程id
			// projectId = Long.valueOf(String.valueOf(conList.get(0).get("id")));
		}
		//	开盘时间
		Date ConstructTime = indent.getDispatchStartTime();
		String tBatch = DateUtil.convertString(ConstructTime, "yyyyMMddHHmm");
		//	截取前3位
		tBatch = tBatch.substring(3);
		//	施工部位
		String site = indent.getSite();
		//	计划方量
		Double RequiredNum = indent.getPlanStere();
		//	浇筑方式
		String PouringMode = indent.getCastModelName();
		//	强度等级
		String ConcreteStrength = indent.getStrengthName();
		//	塌落度
		String Sink = indent.getSlumps();
		//	完成时间
		//	拼接插入任务单是sql
		String insertTaskSql = "insert into " + productTaskTable + " (ID,Code,TStatus,CCode,CName,PCode,PName,TBatch,W_Place,W_Type,CGrade,CSlump,Other,OrderC,OrderM) " +
			"values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		// 拼接参数
		// 查询最大的任务单id
		Long productTaskId = 0l;
		String maxTaskIdSql = "select MAX(id) maxId from " + productTaskTable;
		List<Map<String, Object>> taskDatas = lineJdbcTemp.queryForList(maxTaskIdSql);
		if (taskDatas != null && taskDatas.size() > 0) {
			Map<String, Object> map = taskDatas.get(0);
			Object maxId = map.get("maxId");
			productTaskId = Long.valueOf(maxId.toString());
		}
		//	最大id+1
		productTaskId = productTaskId + 1;
		Object[] taskParams = {productTaskId, indentCode, 0, clientCode, clientName, conCode, conName, tBatch, site, PouringMode, ConcreteStrength, Sink, "", RequiredNum, 0};
		//	砂浆任务单编号
		String standIndentCode = "R" + indentCode;
		//	砂浆id
		Long standProTaskId = productTaskId + 1;
		//	砂浆参数
		Object[] standParams = {standProTaskId, standIndentCode, 0, clientCode, clientName, conCode, conName, tBatch, site, PouringMode, "润管砂浆", Sink, "", RequiredNum, 0};
		//	开启事务
		beginTransaction(lineId);
		//	插入数据
		int update = lineJdbcTemp.update(insertTaskSql, taskParams);
		//	插入砂浆
		lineJdbcTemp.update(insertTaskSql, standParams);
		return update;
	}

}
